//
//  ShowWinScreen.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/21.
//
//

#include "ShowWinScreen.h"
#include "MapMenuItem.h"
#include "MapScreen.h"
#include "TimeBarModel.h"
#include "TitleMenuItem.h"
#include "PlayLevelRecorder.h"
#include "LevelScreen.h"
#include "GoldSaveModel.h"
#include "LocalizedStringGetter.h"
#include "WinStarView.h"
#include "CountDownModeLevelScreen.h"
#define BUTTON_WIDTH 135
#define BUTTON_HEIGHT 50
#define BUTTON_Y      150
#define BACK_WIDTH    50
#define TITLEX        100
#define TITLEY        300
#define TITLE_COLOR   Color3B::RED//Color3B::Color3B(107, 36, 120)
#define TITLE_FONT_SIZE  46
#define TITLE_FONT_NAME  "arial"
#define STAR_Y          400
#define TEXT_FONTSIZE   30
#define TEXT_FONTNAME   "arial"
#define GOLD_TEXT_COLOR  (Color3B::ORANGE)
#define GOLD_ICON_LEFT    100
#define GOLD_ICON_BOTTOM  230
#define GOLD_NUM_LEFT     GOLD_ICON_LEFT+54
#define GOLD_NUM_BOTTOM   GOLD_ICON_BOTTOM+5

typedef enum {
    ZBg = 0,
    ZMenu,
    
} Zindex;

typedef enum {
    TagMenu = 1,
    TagReplay,
    TagNext,
    TagBack,
    
    
} MenuTag;

ShowWinScreen::ShowWinScreen()
{
}



Scene *ShowWinScreen::createScene()
{
    auto scene = Scene::create();
    auto layer = ShowWinScreen::create();
    scene->addChild(layer);
    return scene;
}

bool ShowWinScreen::init()
{
    if (!Layer::init()) {
        return false;
    }
    bgInit();
    titleInit();
    menuInit();
    winStarViewInit();
    goldShowAndSave();
    levelInfoSave();
    return true;
}

void ShowWinScreen::bgInit()
{
    auto bg = Sprite::create("WinBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    this->addChild(bg,ZBg);
}

void ShowWinScreen::winStarViewInit()
{
    Size winSize = Director::getInstance()->getWinSize();
    winStarView = WinStarView::create(TimeBarModel::getInstance()->getStarNum());
    winStarView->setPosition(Point((winSize.width - winStarView->getMySize().width)/2,STAR_Y));
    addChild(winStarView);
}

void ShowWinScreen::titleInit()
{
    Size winSize = Director::getInstance()->getWinSize();
    __String *titleStr = __String::createWithFormat(LocalizedStringGetter::getStringByKey("WinTitle"),PlayLevelRecorder::getInstance()->getNowPlayLevel());
    LabelTTF *title = LabelTTF::create(titleStr->getCString(),TITLE_FONT_NAME , TITLE_FONT_SIZE);
    title->setAnchorPoint(Vec2::ZERO);
    title->setPosition((winSize.width-title->getContentSize().width)/2, TITLEY);
    title->setColor(TITLE_COLOR);
    addChild(title);
}

void ShowWinScreen::menuInit()
{
    Size winsize = Director::getInstance()->getWinSize();
    auto itemReplay = TitleMenuItem::create("WinButtonBg.png", "WinButtonSelected.png","WinButtonBg.png", CC_CALLBACK_1(ShowWinScreen::menuCallback, this));
    itemReplay->setAnchorPoint(Vec2::ZERO);
    itemReplay->setTag(TagReplay);
    itemReplay->setTitle(LocalizedStringGetter::getStringByKey("RePlay"), Size(BUTTON_WIDTH,BUTTON_HEIGHT));
    itemReplay->setPosition(Point((winsize.width/2-BUTTON_WIDTH)/2,BUTTON_Y));
    
    auto itemNext = TitleMenuItem::create("WinButtonBg.png", "WinButtonSelected.png","WinButtonBg.png", CC_CALLBACK_1(ShowWinScreen::menuCallback, this));
    itemNext->setAnchorPoint(Vec2::ZERO);
    itemNext->setTag(TagNext);
    itemNext->setTitle(LocalizedStringGetter::getStringByKey("Continue"), Size(BUTTON_WIDTH,BUTTON_HEIGHT));
    itemNext->setPosition(Point((winsize.width/2-BUTTON_WIDTH)/2+winsize.width/2,BUTTON_Y));
    
    auto itemBack = TitleMenuItem::create("WinBackButton.png", "WinBackButtonSelected.png","WinBackButton.png", CC_CALLBACK_1(ShowWinScreen::menuCallback, this));
    itemBack->setAnchorPoint(Vec2::ZERO);
    itemBack->setTag(TagBack);
    itemBack->setPosition(Point(winsize.width-BACK_WIDTH,winsize.height-BACK_WIDTH));
    
    auto menu = Menu::create(itemReplay,itemNext,itemBack, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
}

void ShowWinScreen::menuCallback(Ref *sender)
{
    TitleMenuItem *item = (TitleMenuItem*)sender;
    if(item->getTag() == TagBack){
        Director::getInstance()->popScene();
    }else if(item->getTag() == TagReplay){
        rePlayDo();
    }else{
        playNextDo();
    }
    
    
}

void ShowWinScreen::rePlayDo()
{
    Director::getInstance()->replaceScene(LevelScreen::createScene(PlayLevelRecorder::getInstance()->getNowPlayLevel()));
}

void ShowWinScreen::playNextDo()
{
	int level = PlayLevelRecorder::getInstance()->getNowPlayLevel();
    if(level+1 <  LevelScreenTypeTypeMax){
        Director::getInstance()->replaceScene(LevelScreen::createScene(level+1));
    }else if(level > CountDownModeLevelScreenTypeNone
    		    &&  level < CountDownModeLevelScreenTypeTypeMax ){
    	Director::getInstance()->replaceScene(CountDownModeLevelScreen::createScene(level+1));
    }else{
        Director::getInstance()->popScene();
    }
}

void ShowWinScreen::goldShowAndSave()
{
    auto goldIcon = Sprite::createWithSpriteFrameName("GoldIcon100.png");
    goldIcon->setAnchorPoint(Vec2::ZERO);
    goldIcon->setPosition(Point(GOLD_ICON_LEFT,GOLD_ICON_BOTTOM));
    this->addChild(goldIcon,ZMenu);

    int goldNumBase = LevelScreenConfig::getWinGold(PlayLevelRecorder::getInstance()->getNowPlayLevel());
    int goldNum = TimeBarModel::getInstance()->getStarNum()*goldNumBase;
    __String *numStr = __String::create("");
    numStr->initWithFormat("%d",goldNum);
    LabelTTF *goldNumLabel = LabelTTF::create(numStr->getCString(), TEXT_FONTNAME, TEXT_FONTSIZE);
    goldNumLabel->setAnchorPoint(Vec2::ZERO);
    goldNumLabel->setPosition(Point(GOLD_NUM_LEFT,GOLD_NUM_BOTTOM));
    goldNumLabel->setColor(GOLD_TEXT_COLOR);
    addChild(goldNumLabel,ZMenu);

    GoldSaveModel::getInstance()->changeGoldNum(goldNum);
}
void ShowWinScreen::levelInfoSave()
{
	int level = PlayLevelRecorder::getInstance()->getNowPlayLevel();
	PlayLevelRecorder::getInstance()->writeLevelPassInfo(level, 1);
	if(PlayLevelRecorder::getInstance()->readLevelStarNumInfo(level) < TimeBarModel::getInstance()->getStarNum()){
		PlayLevelRecorder::getInstance()->writeLevelStarNumInfo(level, TimeBarModel::getInstance()->getStarNum());
	}
}
